<?php

namespace App\Http\Controllers\Reception\Message;

use App\Models\Message;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\URL;
use Illuminate\Http\Request;

//留言模块
class MessageController extends Controller
{

    public function setMessage(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $email = $request->input('email');  //邮箱地址
        //验证邮箱
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $data = fail();
            $data['msg'] = "非法邮箱格式";
            return $data;
        }
        $phone=$request->input('phone');
        if(!verification_phone($phone)){
            $data=fail();
            $data['msg']="非法手机格式";
            return $data;
        }
        $date['user_id'] = (int)$request->input('user_id');
        $date['name'] = $request->input('name');
        $date['phone'] = $phone;
        $date['email'] = $email;
        $date['message'] = $request->input('message');
        $date['time'] = time();
        $msg = Message::insert($date);
        if ($msg) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
}
